package com.IntelligentCommunity.es.controller;

import com.IntelligentCommunity.es.domain.AcOpenlog;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

/**
 * @author 美国五星上将麦克阿瑟
 */
@RestController
@RequestMapping("/demo")
public class AcOpenlogControllerDemo {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    public static final int batchSize = 2000;

    @GetMapping("/createData")
    public void createData(){

        // 生成20万条随机数据
        List<AcOpenlog> acOpenlogs = generateRandomRecords(200000);


        List<CompletableFuture<Void>> futures = new ArrayList<>();

        long start = System.currentTimeMillis();
        System.out.println("开始时间:");
        System.out.println(start);
        for (int i = 0; i < acOpenlogs.size(); i += batchSize) {
            int batchStart = i;
            int batchEnd = Math.min(i + batchSize, acOpenlogs.size());

            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                try {
                    BulkRequest bulkRequest = new BulkRequest();

                    for (int j = batchStart; j < batchEnd; j++) {
                        AcOpenlog record = acOpenlogs.get(j);
                        IndexRequest indexRequest = new IndexRequest("test_index") // 替换为您的索引名称
                                .id(record.getRecordId()) // 可选，指定文档 ID
                                .source(JSON.toJSONString(record), XContentType.JSON);
                        bulkRequest.add(indexRequest);
                    }

                    restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
                } catch (Exception e) {
                    e.printStackTrace(); // 替换为您的日志记录逻辑
                }
            });

            futures.add(future);

        }

        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

        long end = System.currentTimeMillis();
        System.out.println("结束时间:");
        System.out.println(end);

        // 耗时
        System.out.println("耗时:");
        System.out.println(end-start+"ms");



    }

    // 随机生成数据的方法
    public List<AcOpenlog> generateRandomRecords(int count) {

        List<CompletableFuture<Void>> futures = new ArrayList<>();

        List<AcOpenlog> records = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < count; i += batchSize) {

            int batchStart = i;
            int batchEnd = Math.min(i + batchSize, count);
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                for (int j = batchStart; j < batchEnd; j++) {
                    String villageCode = "VC" + String.format("%04d", random.nextInt(10000));
                    String recordId = "R" + String.format("%06d", j);
                    String deviceId = "D" + String.format("%06d", j);
                    String buildingNo = random.nextInt(100) + "-" + random.nextInt(10);
                    String houseNo = String.valueOf(random.nextInt(300));
                    String cardNo = "CARD" + String.format("%05d", j);
                    String credentialType = "ID";
                    String credentialNo = "CREDENTIAL" + String.format("%05d", random.nextInt(100000));
                    String openTime = "2024-08-" + String.format("%02d", random.nextInt(30) + 1) + " " +
                            String.format("%02d:%02d:%02d", random.nextInt(24), random.nextInt(60), random.nextInt(60));
                    String openType = String.valueOf(random.nextInt(1000)); // 随机生成 openType
                    String mrowTime = "2024-08-" + String.format("%02d", random.nextInt(30) + 1) + " " +
                            String.format("%02d:%02d:%02d", random.nextInt(24), random.nextInt(60), random.nextInt(60));

                    records.add(new AcOpenlog(j, villageCode, recordId, deviceId, buildingNo, houseNo, cardNo,
                            credentialType, credentialNo, openTime, openType, mrowTime));
                }
            });
            futures.add(future);

        }

        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

        return records;
    }

}
